package ae.pcfc.cldos.online.net.service.provider;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.soap.SoapMessage;

import ae.pcfc.cldos.online.net.ConstantsNET;
import ae.pcfc.cldos.online.net.service.NetDataTransferService;
import ae.pcfc.cldos.online.net.service.types.ApproveVisitVisaCancel;
import ae.pcfc.cldos.online.net.service.types.ApproveVisitVisaCancelResponse;
import ae.pcfc.cldos.online.net.service.types.BeginEmployeeTermination;
import ae.pcfc.cldos.online.net.service.types.BeginEmployeeTerminationResponse;
import ae.pcfc.cldos.online.net.service.types.BeginEmploymentVisaRenewal;
import ae.pcfc.cldos.online.net.service.types.BeginEmploymentVisaRenewalResponse;
import ae.pcfc.cldos.online.net.service.types.BeginNocAndLetters;
import ae.pcfc.cldos.online.net.service.types.BeginNocAndLettersResponse;
import ae.pcfc.cldos.online.net.service.types.BeginVisitVisaCancel;
import ae.pcfc.cldos.online.net.service.types.BeginVisitVisaCancelResponse;
import ae.pcfc.cldos.online.net.service.types.BeginVisitVisaNew;
import ae.pcfc.cldos.online.net.service.types.BeginVisitVisaNewResponse;
import ae.pcfc.cldos.online.net.service.types.DTOAppMetaData;
import ae.pcfc.cldos.online.net.service.types.DTOEmployee;
import ae.pcfc.cldos.online.net.service.types.DTOPerson;
import ae.pcfc.cldos.online.net.service.types.JVAddRequestEmployee;
import ae.pcfc.cldos.online.net.service.types.JVRequestEmployeeTermination;
import ae.pcfc.cldos.online.net.service.types.JVRequestNocAndLetters;
import ae.pcfc.cldos.online.net.service.types.JVRequestVisitVisaCancel;
import ae.pcfc.cldos.online.net.service.types.JVRequestVisitVisaCancelType;
import ae.pcfc.cldos.online.net.service.types.JVRequestVisitVisaNew;
import ae.pcfc.cldos.online.net.service.types.ObjectFactory;
import ae.pcfc.cldos.online.net.service.types.SendApplicationData;
import ae.pcfc.cldos.online.net.service.types.SendApplicationDataResponse;
import ae.pcfc.cldos.online.net.service.vo.AppMetaDataVO;
import ae.pcfc.cldos.online.net.service.vo.ApplicationDataVO;
import ae.pcfc.cldos.online.net.service.vo.EmployeeVO;
import ae.pcfc.cldos.online.net.service.vo.NocLetterVO;
import ae.pcfc.cldos.online.net.service.vo.PersonVO;
import ae.pcfc.cldos.online.net.service.vo.VisitVisaCancelVO;
import ae.pcfc.cldos.online.net.service.vo.VisitVisaDataVO;
import ae.pcfc.cldos.online.ws.base.BaseWebService;

/**
 * 
 * @author arosa
 *
 */
public class NetDataTransferServiceImpl extends BaseWebService implements NetDataTransferService {

	@Value("${net.url}")
	String classURIValue = ConstantsNET.NET_WS_ENDPOINT;
	
	
	private Map<String, String> empSourceMapping = new HashMap<String, String>();
	
	private Map<String, JVRequestVisitVisaCancelType> exitModeMapping = new HashMap<String, JVRequestVisitVisaCancelType>();
	
	private static final String __EXIT_WITH = "STATUS_YES";
	private static final String __EXIT_WITHOUT = "STATUS_NO";
	private static final String __EXIT_CANCEL = "VISA_TERM_TYPE_CANCEL";
	
	public NetDataTransferServiceImpl(){
		empSourceMapping.put("0", "1");
		empSourceMapping.put("1", "1");
		empSourceMapping.put("2", "2");
		empSourceMapping.put("3", "3");
		
		exitModeMapping.put(__EXIT_WITH, JVRequestVisitVisaCancelType.EXIT_WITH_DOC);
		exitModeMapping.put(__EXIT_WITHOUT, JVRequestVisitVisaCancelType.EXIT_WITH_OUT_DOC);
		exitModeMapping.put(__EXIT_CANCEL, JVRequestVisitVisaCancelType.CANCELATION);
	}
	
	public String transferNewEmploymentVisaData(ApplicationDataVO applicationDataVO){
		ObjectFactory newObjectNetFactory = new ObjectFactory();
		
		DTOAppMetaData dtoAppMetaData = newObjectNetFactory.createDTOAppMetaData();
		AppMetaDataVO appMetaDataVO = applicationDataVO.getAppMetaDataVO();
		getMapper().map(appMetaDataVO, dtoAppMetaData);
		
		DTOEmployee dtoEmployee = newObjectNetFactory.createDTOEmployee();
		EmployeeVO employeeVO = applicationDataVO.getEmployeVO();
		getMapper().map(employeeVO, dtoEmployee);
		dtoEmployee.setEmployeeSource(empSourceMapping.get(employeeVO.getEmployeeSource()));
		
		DTOPerson dtoPerson = newObjectNetFactory.createDTOPerson();
		PersonVO personVO = applicationDataVO.getPersonVO();
		getMapper().map(personVO, dtoPerson);
		dtoPerson.setVisaTypeCode("EPV");
		
		if(dtoEmployee.getAirfare()==null){
			dtoEmployee.setAirfare("0");	
		}
		
		SendApplicationData sendApplicationData = newObjectNetFactory.createSendApplicationData();
		sendApplicationData.setDtoAppMetaData(dtoAppMetaData);
		sendApplicationData.setDtoEmplyee(dtoEmployee);
		sendApplicationData.setDtoPerson(dtoPerson);
		sendApplicationData.setUserID(appMetaDataVO.getInitiatedBy());
		
		SendApplicationDataResponse response = (SendApplicationDataResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, sendApplicationData,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.PUT_APPLICATION_DATA_SOAP_ACTION);
							}
						});
		
		return response.getSendApplicationDataResult();
	}
	
	public String transferRenewEmploymentVisaData(ApplicationDataVO applicationDataVO){
		ObjectFactory newObjectNetFactory = new ObjectFactory();
		
		DTOAppMetaData dtoAppMetaData = newObjectNetFactory.createDTOAppMetaData();
		AppMetaDataVO appMetaDataVO = applicationDataVO.getAppMetaDataVO();
		getMapper().map(appMetaDataVO, dtoAppMetaData);
		
		JVAddRequestEmployee jvAddRequestEmployee = newObjectNetFactory.createJVAddRequestEmployee();
		getMapper().map(applicationDataVO.getRenewEmployeeDataVO(), jvAddRequestEmployee);
		
		BeginEmploymentVisaRenewal beginEmploymentVisaRenewal = 
				newObjectNetFactory.createBeginEmploymentVisaRenewal();
		beginEmploymentVisaRenewal.setAppMetaData(dtoAppMetaData);
		beginEmploymentVisaRenewal.setEmployeData(jvAddRequestEmployee);
		
		BeginEmploymentVisaRenewalResponse response = (BeginEmploymentVisaRenewalResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, beginEmploymentVisaRenewal,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.PUT_RENEW_EMPLOYMENT_VISA_DATA);
							}
						});
		
		return response.getBeginEmploymentVisaRenewalResult();
	}
	
	public String transferTerminateEmploymentVisaData(ApplicationDataVO applicationDataVO){
		ObjectFactory newObjectNetFactory = new ObjectFactory();
		
		DTOAppMetaData dtoAppMetaData = newObjectNetFactory.createDTOAppMetaData();
		AppMetaDataVO appMetaDataVO = applicationDataVO.getAppMetaDataVO();
		getMapper().map(appMetaDataVO, dtoAppMetaData);
		
		JVRequestEmployeeTermination jvRequestEmployeeTermination = newObjectNetFactory.createJVRequestEmployeeTermination();
		getMapper().map(applicationDataVO.getTerminateEmployeeDataVO(), jvRequestEmployeeTermination);
		
		BeginEmployeeTermination beginEmployeeTermination = 
				newObjectNetFactory.createBeginEmployeeTermination();
		beginEmployeeTermination.setDtoAMD(dtoAppMetaData);
		beginEmployeeTermination.setRequest(jvRequestEmployeeTermination);
		
		BeginEmployeeTerminationResponse response = (BeginEmployeeTerminationResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, beginEmployeeTermination,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.PUT_TERMINATE_EMPLOYMENT_VISA_DATA);
							}
						});
		
		return response.getBeginEmployeeTerminationResult();
	}
	
//	public boolean transferExitPaper(ExitPaperTransferVO exitPaperTransferVO){
//		ObjectFactory newObjectNetFactory = new ObjectFactory();
//		
//		EmpTermUploadExitPaper empTermUploadExitPaper = new EmpTermUploadExitPaper();
//		
//		ArrayOfAttachmentInfo arrayOfAttachmentInfo = newObjectNetFactory.createArrayOfAttachmentInfo();
//		List<AttachmentVO> attachmentsList = exitPaperTransferVO.getAttachmentVOList();
//		for(AttachmentVO attachmentVO : attachmentsList){
//			AttachmentInfo attachmentInfo = newObjectNetFactory.createAttachmentInfo();
//			getMapper().map(attachmentVO, attachmentInfo);
//			arrayOfAttachmentInfo.getAttachmentInfo().add(attachmentInfo);
//		}
//		empTermUploadExitPaper.setAttachments(arrayOfAttachmentInfo);
//		empTermUploadExitPaper.setOfflineAppId(exitPaperTransferVO.getOfflineAppId());
//		
//		EmpTermUploadExitPaperResponse response = (EmpTermUploadExitPaperResponse) wsTemplate
//				.marshalSendAndReceive(classURIValue, empTermUploadExitPaper,
//						new WebServiceMessageCallback() {
//							public void doWithMessage(WebServiceMessage message) {
//								((SoapMessage) message)
//										.setSoapAction(ConstantsNET.UPLOAD_EXIT_PAPER);
//							}
//						});
//		
//		return response.isEmpTermUploadExitPaperResult();
//	}
	
	//Added by Asna for new Visit Visa transfer
	/**
	 * @param applicationDataVO
	 * @return
	 */
	public String transferNewVisitVisaData(ApplicationDataVO applicationDataVO){
		ObjectFactory newObjectNetFactory = new ObjectFactory();
		
		DTOAppMetaData dtoAppMetaData = newObjectNetFactory.createDTOAppMetaData();
		AppMetaDataVO appMetaDataVO = applicationDataVO.getAppMetaDataVO();
		getMapper().map(appMetaDataVO, dtoAppMetaData);
		
		JVRequestVisitVisaNew visitVisaReq = newObjectNetFactory.createJVRequestVisitVisaNew();
		
		EmployeeVO employeeVO = applicationDataVO.getEmployeVO();
		getMapper().map(employeeVO, visitVisaReq);
		
		PersonVO personVO = applicationDataVO.getPersonVO();
		getMapper().map(personVO, visitVisaReq);
		
		VisitVisaDataVO visaDataVO  = applicationDataVO.getVisitVisaDataVO();
		getMapper().map(visaDataVO, visitVisaReq);

		BeginVisitVisaNew visitVisaNew = newObjectNetFactory.createBeginVisitVisaNew();
		visitVisaNew.setDtoAMD(dtoAppMetaData);
		visitVisaNew.setRequest(visitVisaReq);
		
		BeginVisitVisaNewResponse response = (BeginVisitVisaNewResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, visitVisaNew,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.PUT_NEW_VISIT_VISA_DATA);
							}
						});
		
		return response.getBeginVisitVisaNewResult();
	}

	public String transferCancelVisaData(ApplicationDataVO applicationDataVO){
		ObjectFactory newObjectNetFactory = new ObjectFactory();
		
		DTOAppMetaData dtoAppMetaData = newObjectNetFactory.createDTOAppMetaData();
		AppMetaDataVO appMetaDataVO = applicationDataVO.getAppMetaDataVO();
		getMapper().map(appMetaDataVO, dtoAppMetaData);
		
		JVRequestVisitVisaCancel jvRequestVisitVisaCancel = new JVRequestVisitVisaCancel();
		VisitVisaCancelVO visitVisaCancelVO = applicationDataVO.getVisitVisaCancelVO();
		getMapper().map(visitVisaCancelVO, jvRequestVisitVisaCancel);
		jvRequestVisitVisaCancel.setExitMode(exitModeMapping.get(visitVisaCancelVO.getVvcExitMode()));
		
		BeginVisitVisaCancel beginVisitVisaCancel = 
				newObjectNetFactory.createBeginVisitVisaCancel();
		beginVisitVisaCancel.setDtoAMD(dtoAppMetaData);
		beginVisitVisaCancel.setRequest(jvRequestVisitVisaCancel);
		
		BeginVisitVisaCancelResponse response = (BeginVisitVisaCancelResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, beginVisitVisaCancel,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.PUT_CANCEL_VISIT_VISA_DATA);
							}
						});
		
		return response.getBeginVisitVisaCancelResult();
	}
	

	
	public String transferNocAndLettersData(ApplicationDataVO applicationDataVO) {
		ObjectFactory newObjectNetFactory = new ObjectFactory();
		
		DTOAppMetaData dtoAppMetaData = newObjectNetFactory.createDTOAppMetaData();
		AppMetaDataVO appMetaDataVO = applicationDataVO.getAppMetaDataVO();
		getMapper().map(appMetaDataVO, dtoAppMetaData);
		dtoAppMetaData.setProcessCode("NI");
		
		JVRequestNocAndLetters jJVRequestNocAndLetters = new JVRequestNocAndLetters();
		NocLetterVO nocLetterVO = applicationDataVO.getNocLetterVO();
		jJVRequestNocAndLetters.setEmployeeID(nocLetterVO.getNlEmployeeId());
		jJVRequestNocAndLetters.setNocType(nocLetterVO.getNlNocLetterType());
		
		BeginNocAndLetters beginNocAndLetters = 
				newObjectNetFactory.createBeginNocAndLetters();
		beginNocAndLetters.setDtoAMD(dtoAppMetaData);
		beginNocAndLetters.setRequest(jJVRequestNocAndLetters);
		
		BeginNocAndLettersResponse response = (BeginNocAndLettersResponse) wsTemplate
				.marshalSendAndReceive(classURIValue, beginNocAndLetters,
						new WebServiceMessageCallback() {
							public void doWithMessage(WebServiceMessage message) {
								((SoapMessage) message)
										.setSoapAction(ConstantsNET.PUT_NEW_NOC_LETTERS);
							}
						});
		return response.getBeginNocAndLettersResult();
	}
	
}
